﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DTO;
using WebClient;
using System.Data;
using System.Collections;
namespace WebClient.admin.Modules.bill
{
    public partial class AddNew : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.btnAddNew.Attributes.Add("onclick", "return OnSubmitClick();");
            this.btnCancel.Attributes.Add("onclick", "return ClearAll()");
            if (!this.IsPostBack) {
                loadDataCbb();
                ViewState["URLReferrer"] = Request.UrlReferrer.AbsoluteUri;
            }

        }

        private void AddNewItem()
        {
            WebData data = new WebData();
            Bill item = new Bill();
            if (Session["detailBill"] == null)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Err", "alert('" + " Chưa nhập mặt hàng nào cả " + "');", true);
                return;
            }
            //item.id =Convert this.txtid.Text.Trim();
            item.customersId = Convert.ToInt64(this.cbbcustomersId.SelectedValue);
            item.address = this.txtaddress.Text.Trim();
            item.create_on = DateTime.Now;
            item.cardId = 1;
            item.total = Convert.ToDecimal(this.txttotal.Text.Trim());
            item.discount = 0;
            item.payment = Convert.ToDecimal(this.txtpayment.Text.Trim());
            item.arrears = 0;
            item.credit = "";// this.txtcredit.Text.Trim();
            item.status = 1;
            item.orderId = Convert.ToInt32(this.txtorderId.Text.Trim());
            item.isActive = 1;
            data.OnSubmitBill(ref item, 0);
            if (Session["detailBill"] != null)
            {
                ArrayList list = (ArrayList)Session["detailBill"];
                foreach (DTO.Billdetail bill in list)
                {
                    bill.billId = item.id;
                    data.OnSubmitBilldetail(bill, 0);
                }
                Session.Remove("detailBill");
                Response.Redirect(ViewState["URLReferrer"].ToString());
            }

        }

        private void loadDataCbb() {
            WebData db = new WebData();
            //Nha cung cap
            this.cbbcustomersId.DataSource = db.GetAllCustomers();
            this.cbbcustomersId.DataTextField = DTO.Customers.c_fullname;
            this.cbbcustomersId.DataValueField = DTO.Customers.c_id;
            this.cbbcustomersId.DataBind();
            ///// san pham
            this.cbbproductId.DataSource = db.GetAllProducts();
            this.cbbproductId.DataValueField = DTO.Products.c_id;
            this.cbbproductId.DataTextField = DTO.Products.c_productName;
            this.cbbproductId.DataBind();
        }

        private void detailBill() {
            //DataTable tb = new DataTable();
            ArrayList list = null;
            DTO.Billdetail item = new Billdetail();
            item.billId = 1;
            item.discount = 0;
            item.numberOf = Convert.ToInt32(txtnumberOf.Text);
            item.orderId = 1;
            item.productId = Convert.ToInt32(cbbproductId.SelectedValue);
            item.productName = cbbproductId.SelectedItem.Text;
            item.unitPriceUSD = Convert.ToDecimal(txtunitPriceUSD.Text);
            item.unitPriceVND = Convert.ToDecimal(txtunitPriceVND.Text);
            if (Session["detailBill"] == null) {
                list = new ArrayList();
                list.Add(item);
            }
            else {
                list = (ArrayList)Session["detailBill"];
                int i = index(list, item.productId);
                if (i != -1) {
                    DTO.Billdetail bill = (DTO.Billdetail)list[i];
                    bill.numberOf += item.numberOf;
                }
                else
                    list.Add(item);
            }
            Session["detailBill"] = list;
            this.grdDs.DataSource = list;
            this.grdDs.DataBind();
        }

        private int index(ArrayList list, long id) {
            int i = -1;
            foreach (DTO.Billdetail bill in list) {
                i++;
                if (bill.productId == id)
                    return i;
            }
            return -1;
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            AddNewItem();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            detailBill();
        }

    }
}